package org.vaadin.vaadinjit.widgetset.client.ui;

import com.google.gwt.core.client.JavaScriptObject;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.UIDL;

/**
 * Client side widget which communicates with the server. Messages from the
 * server are shown as HTML and mouse clicks are sent to the server.
 */
public class VJitAreaChart extends VJitChartWidget<JitAreaChartOverlay> {

	/** Set the CSS class name to allow styling. */
	public static final String CLASSNAME = "v-jitwrapper";
	
	/** UIDL identifier for filtering stack */
	public static String FILTER_STACK_IDENTIFIER = "filterStack";

	/** When this is true the stacks are filtered on clicks */
	private boolean filterOnClick = true;
	
	/** The filtered stack, is null if no stack is filtered  */
	private String filteredStack = null;
	
	
	/**
	 * The constructor should first call super() to initialize the component and
	 * then handle any initialization relevant to Vaadin.
	 */
	public VJitAreaChart() {
		super();
	}

	/**
	 * Called whenever an update is received from the server 
	 */
	@Override
	public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
		super.updateFromUIDL(uidl, client);
		
		// Filter the selected stack
		if (uidl.hasAttribute("filterStack")) {
			String label = uidl.getStringAttribute("filterStack");
			filteredStack = label;
			getVisualization().filter(label);
		}
		
		if (uidl.hasAttribute("restoreStack")) {
			filteredStack = null;
			getVisualization().restore();
		}
		
		if (uidl.hasAttribute("filterOnClick")) {
			boolean filterOnClick = uidl.getBooleanAttribute("filterOnClick");
			this.filterOnClick = filterOnClick;
		}
		
		
	}
	
	/**
	 * This method is ran when JIT fires a click event on a stack
	 * 
	 * @param stack the label of the stack
	 */
	public void onClickStack(String stack) {
		if (filterOnClick) {
			ApplicationConnection.getConsole().log("Stack clicked: " + stack);
			filteredStack = stack;
			getVisualization().filter(stack);
			client.updateVariable(paintableId, FILTER_STACK_IDENTIFIER, stack, true);
		}
	}
	
	
	@Override
	protected void setupConfig(JitConfiguration config) {
		super.setupConfig(config);
		
		// Set basic options to default values
		config.setOption("labelOffset", 10);
		config.setOption("showAggregates", true);
		config.setOption("showLabels", true);
		config.setOption("filterOnClick", false);
		config.setOption("filterOnRightClick", false);
		
		JitConfiguration label = new JitConfiguration();
		label.setOption("type", getLabelType());
		label.setOption("size", 13);
		label.setOption("family", "Arial");
		label.setOption("color", getTextColor());
		
		config.setOption("Label", label);
			
	}
	
	
	protected native JitAreaChartOverlay initJit(JavaScriptObject jitConfig) /*-{
		var elName = jitConfig.injectInto;
		var tipsJson = {
			enable: true,
			onShow: function(tip, elem) {
				$wnd.$jit.fireChartHoverLabel(elName, elem.name);
				tip.innerHTML = "<b>" + elem.name + "</b>: " + elem.value;
			}
		};
		var eventsJson = {
			enable: true,
			type: 'Native',
			onClick: function(node, eventInfo, evt) {
				var elem = eventInfo.getContains();
				if (elem) $wnd.$jit.fireAreaChartClick(elName, evt, elem.name);
			}
		};
		jitConfig['Tips'] = tipsJson;
		jitConfig['Events'] = eventsJson;
		return new $wnd.$jit.AreaChart(jitConfig);
	}-*/;
	


	
	@Override
	protected native void handleNativeResize(JitAreaChartOverlay overlay) /*-{
	    var size = overlay.st.canvas.getSize(),
        margin = overlay.config.Margin;
    	overlay.st.config.offsetY = -size.height/2 + margin.bottom 
      		+ (overlay.config.showLabels 
      			&& (overlay.config.labelOffset + overlay.config.Label.size));
    	
    	overlay.st.config.offsetX = (margin.right - margin.left)/2;
    	
    	overlay.st.refresh();
    	
    	overlay.normalizeDims();
    	overlay.st.plot();
    	
    	overlay.st.fx.animate({
      		modes: ['node-property:height:dimArray'],
      		duration:1500
      	});
    	
	}-*/;
	
}
